/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package testcase

import (
	"fmt"
	httpexpect "github.com/gavv/httpexpect/v2"
	"net/http"
	"strconv"

	//. "saas_sys_base/modules/user/service"
	. "gopkg.in/check.v1"
	"testing"
)

var a int = 1

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type MySuite struct{}

var _ = Suite(&MySuite{})

func (s *MySuite) SetUpSuite(c *C) {
	/*str3 := "第1次套件开始执行"
	fmt.Printf("%s, %s", str3, c.TestName())
	c.Logf(str3)*/

}

func (s *MySuite) TearDownSuite(c *C) {
	str4 := "第1次套件执行完成"
	fmt.Println(str4)
}

func (s *MySuite) SetUpTest(c *C) {
	str1 := "第" + strconv.Itoa(a) + "条用例开始执行"
	fmt.Println(str1)

}

func (s *MySuite) TearDownTest(c *C) {
	str2 := "第" + strconv.Itoa(a) + "条用例执行完成"
	fmt.Println(str2)
	a = a + 1

}

func (s *MySuite) T2estHello(c *C) {
	str2 := "TearHello"
	fmt.Println(str2)
	c.Fail()

}

func (s *MySuite) TestUserLogin22(c *C) {
	//t.Logf("Hello() = %v, want %v", 1, 2)
	type Request struct {
		Code  string `json:"code"`
		Tid   int64  `json:"tid,omitempty"`
		AppId int64  `json:"appid,omitempty"` //内部app id, internal app id
	}

	//url querystring格式定义
	type UrlParam struct {
		Code  string `url:"code"`
		Tid   int64  `url:"tid"`
		AppId int64  `url:"appid"`
	}

	type Resp struct {
		//*LoginUser
		Token string
	}

	var req Request
	var urlParam UrlParam
	//var resp Resp
	testurl := "http://localhost:10000"
	//urlstring := make([]byte, 0)
	urlParam.AppId = 1000
	urlParam.Code = "221000"
	urlParam.Tid = 11
	req.AppId = 200
	//urlstring, _ := urlenc.Marshal(&urlParam)
	//第一种写法：未有任何封装
	/*e := httpexpect.New(t, testurl) //创建一个httpexpect实例

	e.POST("/api/user/login").WithQueryObject(urlParam).WithJSON(req).
		Expect().
		Status(http.StatusOK). //判断请求是否200
		JSON().
		Object().                   //json body实例化
		ContainsKey("msg").         //检验是否包括key
		ValueEqual("msg", "online") //对比key的value*/
	//第二种写法：
	//分为两步，一个是创建并发送测试请求
	test := NewTestPostDefault(c, testurl, "/api/user/login", urlParam, nil)
	test = test
	//第二步是对比测试结果
	test.Expect().
		Status(http.StatusOK).
		JSON().Object().
		ContainsKey("token").
		ValueEqual("code", 0) // .Equal(resp)

	//第三种写法
	/*test = NewTestPostDefault(t, "/api/user/login", urlParam, req)
	result := ResultTest(t, test, http.StatusOK, nil)
	result.ValueNotEqual("token", "")

	//第四种写法
	reqpara := ReqTestPara{
		TestUrl: "/api/user/login",
		Query:   urlParam,
		Jsonobj: req}

	RunPostTest(t, reqpara, http.StatusOK, nil)*/

}

func TestUserLogin(t *testing.T) {
	//t.Logf("Hello() = %v, want %v", 1, 2)
	type Request struct {
		Code  string `json:"code"`
		Tid   int64  `json:"tid,omitempty"`
		AppId int64  `json:"appid,omitempty"` //内部app id, internal app id
	}

	//url querystring格式定义
	type UrlParam struct {
		Code  string `url:"code"`
		Tid   int64  `url:"tid"`
		AppId int64  `url:"appid"`
	}

	type Resp struct {
		//*LoginUser
		Token string
	}

	var req Request
	var urlParam UrlParam
	//var resp Resp
	testurl := "http://localhost:10000"
	//urlstring := make([]byte, 0)
	urlParam.AppId = 1000
	urlParam.Code = "221000"
	urlParam.Tid = 11
	req.AppId = 200
	//urlstring, _ := urlenc.Marshal(&urlParam)
	//第一种写法：未有任何封装
	/*e := httpexpect.New(t, testurl) //创建一个httpexpect实例

	e.POST("/api/user/login").WithQueryObject(urlParam).WithJSON(req).
		Expect().
		Status(http.StatusOK). //判断请求是否200
		JSON().
		Object().                   //json body实例化
		ContainsKey("msg").         //检验是否包括key
		ValueEqual("msg", "online") //对比key的value*/
	//第二种写法：
	//分为两步，一个是创建并发送测试请求
	test := NewTestPostDefault(t, testurl, "/api/user/login", urlParam, nil)
	test = test
	//第二步是对比测试结果
	test.Expect().
		Status(http.StatusOK).
		JSON().Object().
		ContainsKey("token").
		ValueEqual("code", 0) // .Equal(resp)

	//第三种写法
	/*test = NewTestPostDefault(t, "/api/user/login", urlParam, req)
	result := ResultTest(t, test, http.StatusOK, nil)
	result.ValueNotEqual("token", "")

	//第四种写法
	reqpara := ReqTestPara{
		TestUrl: "/api/user/login",
		Query:   urlParam,
		Jsonobj: req}

	RunPostTest(t, reqpara, http.StatusOK, nil)*/

}

func NewTestPostDefault(t httpexpect.LoggerReporter, testurl string, testpath string, query interface{}, jsonobj interface{}) *httpexpect.Request {
	e := httpexpect.New(t, testurl)
	request := e.POST(testpath).WithQueryObject(query).WithJSON(jsonobj)
	return request
}

func GetResp(t *testing.T, testReq *httpexpect.Request, status int, resultExpcted interface{}) *httpexpect.Object {
	obj := testReq.Expect().Status(status).JSON().Object()
	if resultExpcted != nil {
		obj.Equal(resultExpcted)
	}
	return obj
}

type ReqTestPara struct {
	TestUrl  string
	TestPath string
	Query    interface{}
	Jsonobj  interface{}
}

func RunPostTest(t *testing.T, req ReqTestPara, status int, resultExpcted interface{}) {
	test := NewTestPostDefault(t, req.TestUrl, req.TestPath, req.Query, req.Jsonobj)
	GetResp(t, test, http.StatusOK, resultExpcted)
}

func TestUserLogin_004(t *testing.T) {

}

func TestUserLogin_005(t *testing.T) {

}
